<template>
  <PageLayout>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">内容管理</div>
        <a-space :size="16">
          <a-button type="primary" @click="edit()">新增广告</a-button>
          <!-- <a-button>导入</a-button> -->
        </a-space>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="apiFetch"  -->
        <BasicTable
          ref="tableRef"
          :columns="columns"
          :query-params="queryData"
          :apiFetch="postBannerPage"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'imageUrl'">
              <a-image class="custom-img"  :width="50" :height="50" :src="record.imageUrl" />
            </template>
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="edit(record)">编辑</a>
                <a class="btn" @click="remove(record.id)">删除</a>
                <a class="btn" @click="topBanner(record)">置顶 </a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageLayout>
</template>

<script name="content-list" setup>
import {
  postBannerPage,
  postBannerDelete,
  postBannerTop,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const storeCommonEnum = useStoreCommonEnum();
const router = useRouter();
const queryOptions = ref([
  {
    name: "标题",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "title",
  },
]);
const columns = [
  {
    title: "序号",
    dataIndex: "index",
    width: "120px",
    customRender: (record) => `${record.index + 1}`,
  },
  {
    title: "缩略图",
    dataIndex: "imageUrl",
    key: "imageUrl",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "标题",
    dataIndex: "title",
    key: "title",
    // width: "150px",
    ellipsis: true,
  },
  // {
  //   title: "类型",
  //   dataIndex: "nickname",
  //   key: "nickname",
  //   width: "150px",
  //   ellipsis: true,
  // },
  // {
  //   title: "状态",
  //   dataIndex: "status",
  //   key: "status",
  //   width: "150px",
  //   ellipsis: true,
  //   customRender: ({ record }) => (record.status == 0 ? "正常" : "停用"),
  // },
  {
    title: "首发时间",
    dataIndex: "createTime",
    key: "createTime",
    // width: "150px",
    ellipsis: true,
  },
  {
    title: "更新时间",
    dataIndex: "updateTime",
    key: "updateTime",
    // width: "150px",
    ellipsis: true,
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const edit = (record) => {
  router.push({
    path: "/content/edit",
    query: {
      id: record?.id,
    },
  });
};

const topBanner = (record) => {
  comfirmModal({
    content: `确认置顶`,
    onOk() {
      postBannerTop({
        id: record.id,
      }).then(() => {
        message.success('置顶成功');
        tableRef.value.fetch();
      });
    },
  });
};
const remove = (id) => {
  comfirmModal({
    content: "确认删除该广告",
    onOk() {
      postBannerDelete({ id }).then(() => {
        message.success("删除成功");
        tableRef.value.fetch();
      });
    },
  });
};

const dataSource = ref(
  Array.from({ length: 11 }, () => ({
    spaceName: "2121",
  }))
);
</script>

<style lang="less" scoped>
:deep(.custom-img) {
  width: 50px;
  height: 50px;
  object-fit: cover;
}
</style>
